package com.example.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.codingapi.tx.framework.utils.SerializerUtils;
import com.codingapi.tx.model.TransactionInvocation;
import com.example.demo.entity.Test;
import com.example.demo.service.DemoService;
import com.lorne.core.framework.utils.encode.Base64Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.TypeVariable;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created by lorne on 2017/6/26.
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;
    private static Test test = new Test(23);

    @RequestMapping("/list")
    @ResponseBody
    public List<Test> list() {

        return demoService.list();
    }


    @RequestMapping("/save")
    @ResponseBody
    public int save(String name, String id) throws InterruptedException, IOException {
       /* demoService.save(id, name);
        ThradsingletonInt.set(num);
        ThradsingletonInt.set(num + ThradsingletonInt.get());
        System.out.println("开始" + name);
        Thread.sleep(6000);
        System.out.println("结束");
        return 2;*/
        test = new Test(24);
        Test test = new Test(23);
        Field[] fields = Test.class.getDeclaredFields();
        Method[] methods = Test.class.getMethods();
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(0);
        arrayBlockingQueue.poll();
        //Collections.synchronizedMap();
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(192), 23);
        ServerSocket serverSocket = new ServerSocket(5862);
        return demoService.save(id, name);
    }

    @PostMapping("notifyResult")
    public String notifyResult(@RequestBody String jsonStr) {
        System.out.println("通知地址..." + jsonStr);
        String data = (String) JSONObject.parseObject(jsonStr).get("json");
        data = (String) JSONObject.parseObject(data).get("data");
        byte[] serializers = Base64Utils.decode(data);
        TransactionInvocation transactionInvocation = SerializerUtils.parserTransactionInvocation(serializers);
        serializers.notifyAll();
        System.out.println(transactionInvocation.getMethodStr());
        return null;
    }
}
